package kr.hkit.powerjava.chapter22.work2.doa;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import kr.hkit.powerjava.chapter22.work2.vc.Student;

public class StudentManagerImpl implements ItemDaoInterface<Student> {
	private ArrayList<Student> arList;
	
	public StudentManagerImpl(ArrayList<Student> stdList) {

		this.arList = stdList;
	}

	@Override
	public void addItem(Student item) {
		arList.add(item);
	}

	@Override
	public void delItem(Student item) {
		arList.remove(item);
	}

	@Override
	public void updateItem(Student item) {
		if(searchItem(item)){
		arList.set(arList.indexOf(item), item);
		} else {
			System.out.println("수정실패");
		}

	}

	@Override
	public boolean searchItem(Student item) {
		return arList.contains(item);
	}

	@Override
	public void prnItems() {
		prnHeader();
//		Collections.sort(arList);
		for(Student s : arList) {
			System.out.println(s);
		}
		System.out.println();
	}
	
	
	private void prnHeader() {
		String header = String
		.format("%3s\t %5s\t %3s\t %3s\t %3s\t %3s\t %3s\t",
				"학번", "성명", "국어", "영어", "수학", "총점", "평균");
		System.out.println(header);
		System.out.println("---------------------------------------------------------------");
	}

	@Override
	public void prnItems(Comparator<Student> comp) {
		Collections.sort(arList, comp);
		prnItems();
	}
}
